package com.zhengke.controller.hubf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.zhengke.common.util.FileExport;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.pojo.TvGoodsStockInventory;
import com.zhengke.service.IInventoryManagementService;

@Controller
@RequestMapping("/iinventory")
public class IInventoryManagementController {
	
	@Autowired
	IInventoryManagementService iinventoryManagermentService;
	
	//创建返回的界面的请求
	@RequestMapping("/gotoManagerJSP.do")
	public String gotoManagerJSP() {
		return "/iinventoryManage/iiventoryManagement";
	}
	
	//查询所有的库存的方法
	@RequestMapping("/getData.do")
	@ResponseBody
	public SysOrderResult getOrderListMethod(String goods_name,String goods_type,String inventory_name,String inventory_after,String inventory_before,int page,int limit) {
		SysOrderResult sysOrderResult = null;
		System.out.println("inventory_after===="+inventory_after+"inventory_timebefore"+inventory_before);
		try {
		PageHelper.startPage(page,limit,true);
		//往集合中装数据
		Map<String,Object> messageMap = new HashMap<String,Object>();
		messageMap.put("goods_name", goods_name);
		messageMap.put("goods_type", goods_type);
		messageMap.put("inventory_name", inventory_name);
		messageMap.put("","");
		messageMap.put("inventory_timeafter", inventory_after);
		messageMap.put("inventory_timebefore", inventory_before);
		List<TvGoodsStockInventory> tvGoodsList = iinventoryManagermentService.goodsStockInventorieList(messageMap);
		System.out.println("<===================================================>"+tvGoodsList);
		int total = tvGoodsList.size();
		Long pageTotal = new Long((long)total);
		sysOrderResult = new SysOrderResult();
		sysOrderResult.setCode("0");
		sysOrderResult.setCount(pageTotal);
		sysOrderResult.setData(tvGoodsList);
		}catch(Exception e) {
			e.getStackTrace();
		}
		return sysOrderResult;
	}
	
	/**
	 * 导出excell的方法1，导出全部的商品
	 */
	@RequestMapping("/exportOne.do")
	@ResponseBody
	public void exportExcellOne(String goods_id,String goods_name,String inventory_name,String inventory_time,HttpServletResponse response) {
		//验证
		System.out.println("goods_id===>"+goods_id+"goods_name===>"+goods_name+"inventory_name===>"+inventory_name+"inventory_time===>"+inventory_time);
		try {
		//根据传输过来的内容进行查询内容
		Map<String,Object> messageMap = new HashMap<String,Object>();
		messageMap.put("goods_name", goods_name);
		messageMap.put("inventory_name", inventory_name);
		List<TvGoodsStockInventory> tvGoodsList = iinventoryManagermentService.goodsStockInventorieList(messageMap);
		if(tvGoodsList != null) {
			String[] tableTitle = { "库存商品id", "库存商品的名称", "库存商品的盘点人" };
			List<Object[]> objectList = new ArrayList<Object[]>();
			for(TvGoodsStockInventory goodsInventory : tvGoodsList){
				Object[] obj = new Object[tableTitle.length];// 创建对应表头长度的数组用来装数据
				obj[0] = goodsInventory.getGoods_id();
				obj[1] = goodsInventory.getGoods_name();
				obj[2] = goodsInventory.getInventory_name();
				objectList.add(obj);
			}
			//导出表格
			FileExport.exportExcel(tableTitle, objectList, "库存订单信息", response);
		}
		}catch (Exception e) {
		}
		/**
		 * date_formate
		 * goods_id="+$("#goods_id").val()
		 * goods_name="+$("#goods_name").val()
		 * inventory_name="+$("#inventory_name").val()
		 * inventory_time="+$("#inventory_time").val();
		 */
	}
	
		@RequestMapping("/exportExcellTwo.do")
		@ResponseBody
		public void exportExcellTwo(String data,HttpServletResponse response) {
				if(!(data == null)) {
					try {
						String[] tableTitle = { "库存商品id", "库存商品的名称","盘点时间", "库存商品的盘点人" };
						// 创建最后的集合用来传输
						List<Object[]> objectList = new ArrayList<Object[]>();	
						//将传输过来的数据进行json格式化的转化
						List<TvGoodsStockInventory> tvGoodsList = JSONObject.parseArray(data,TvGoodsStockInventory.class);
						//遍历集合添加元素
						for(TvGoodsStockInventory goodsInventory : tvGoodsList){
							Object[] obj = new Object[tableTitle.length];// 创建对应表头长度的数组用来装数据
							obj[0] = goodsInventory.getGoods_id();
							obj[1] = goodsInventory.getGoods_name();
							obj[2] = goodsInventory.getInventory_time();
							obj[3] = goodsInventory.getInventory_name();
							objectList.add(obj);
						}
						FileExport.exportExcel(tableTitle, objectList, "库存商品信息新", response);
					}catch (Exception e) {
						
					}
				}
		}
}
